import {
  tmsDriverPage,
  tmsDriverInsertOrUpdate,
  tmsDriverDel,
  selectList,
} from '../services/driver';
import React from 'react';
import {Icon, Input, message, Switch, Tooltip} from 'antd';

/**
 * 客户数据模型
 */

//表格列表信息
const columns = [
  {
    title: '司机姓名',
    dataIndex: 'driver',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '车牌号',
    dataIndex: 'carNo',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '司机电话',
    dataIndex: 'drPhone',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '身份证号',
    dataIndex: 'drIdcard',
    width: '100px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '账号姓名',
    dataIndex: 'accountName',
    width: '100px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '银行账号',
    dataIndex: 'bankAccount',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '是否拉黑',
    width: '120px',
    dataIndex: 'isLock',
    editable: true,
    ellipsis: true,
    render: (text, record, index) =>{
      return (
        <Switch
          checkedChildren = {<Icon type="check" />}
          unCheckedChildren = {<Icon type="close" />}
          checked = {record.isLock === 1}
        />)
    }
  }
];

// 搜索框字段
const searchFields = [
  {
    cols: [
      {
        md: 6,
        label: '司机姓名',
        name: 'driver',
        component: <Input placeholder="请输入司机姓名" />,
      },
      {
        md: 6,
        label: '车牌号',
        name: 'carNo',
        component: <Input placeholder="请输入车牌号" />,
      },
    ],
  },
];

// 校验规则
const requiredCols = {
  司机姓名: [
    {
      required: 'true',
      message: `请输入司机姓名!`,
    },
  ],
};

export default {
  namespace: 'driver',

  state: {
    searchFields: searchFields, // 搜索框信息
    searchValues: {}, // 搜索框里的值
    pageList: [], // 表格数据
    pagination: {
      pageSize: 10,
      current: 1,
      size: 'small',
      showSizeChanger: true,
      showQuickJumper: true,
    }, // 表格分布数据
    columns: columns, // 表格列信息
    editId: '', // 编辑行id
    selectionIds: [], // 表格选择ID
    requiredCols: requiredCols, // 必填字段
    driverList: [], // 司机下拉选择
  },

  effects: {
    // 获取分页数据
    *page({ payload }, { call, put }) {
      const res = yield call(tmsDriverPage, payload);
      yield put({
        type: 'page_r',
        payload: res,
      });
    },
    *save({ payload }, { call, put }) {
      const res = yield call(tmsDriverInsertOrUpdate, payload);
      yield put({
        type: 'save_r',
        payload: res,
      });
    },
    *del({ payload }, { call, put }) {
      const res = yield call(tmsDriverDel, payload);
      yield put({
        type: 'del_r',
        payload: res,
      });
    },
    *selectList({ payload }, { call, put }) {
      const res = yield call(selectList, payload);
      yield put({
        type: 'selectList_r',
        payload: res,
      });
    },
  },

  reducers: {
    // 新增
    handleAdd(state) {
      const { pageList, pagination } = state;
      let addList = pageList.filter(page => {
        return page.id === 0;
      });
      if (addList.length) {
        message.warning('请提交后再添加！');
        return {
          ...state,
        };
      } else {
        pageList.unshift({ id: 0, customerName: '新增' });
        const newPagination = {
          pages: pagination.pages,
          pageSize: pagination.pageSize,
          total: pagination.total + 1,
          current: pagination.current,
          size: 'small',
          showSizeChanger: true,
          showQuickJumper: true,
        };
        return {
          ...state,
          pageList: pageList,
          pagination: newPagination,
        };
      }
    },
    // 绑定搜索框数据
    handleSearchChange(state, action) {
      const { searchValues } = state;
      const newSearchForm = {
        ...searchValues,
        ...action.payload,
      };
      return {
        ...state,
        searchValues: newSearchForm,
      };
    },
    // 处理编辑
    handleEdit(state, action) {
      return {
        ...state,
        editId: action.payload,
      };
    },

    // 改变分页插件
    handlePagination(state, action) {
      const { payload } = action;
      const { pagination } = state;
      let newPagination = {
        ...pagination,
        ...payload,
      };
      return {
        ...state,
        pagination: newPagination,
      };
    },

    // 表格选中Id
    handleSelection(state, action) {
      const { payload } = action;
      return {
        ...state,
        selectionIds: payload,
      };
    },
    // 分页数据请求回来后处理
    page_r(state, action) {
      const { payload } = action;
      if (payload && payload.code === 0) {
        const list = payload.result.records;
        const pagination = {
          pages: payload.result.pages,
          pageSize: payload.result.size,
          total: payload.result.total,
          current: payload.result.current,
          size: 'small',
          showSizeChanger: true,
          showQuickJumper: true,
        };
        return {
          ...state,
          pagination: pagination,
          pageList: list,
          editId: '',
        };
      } else {
        return {
          ...state,
        };
      }
    },
    // 处理保存
    save_r(state, action) {
      const { payload } = action;
      if (payload && payload.code === 0) {
        message.success('保存成功！');
      } else {
        message.error(`${payload.errorMsg}` ? `${payload.errorMsg}` : '保存失败！');
      }
      return {
        ...state,
      };
    },
    // 处理删除
    del_r(state, action) {
      const { payload } = action;
      if (payload && payload.code === 0) {
        message.success('删除成功！');
      } else {
        message.error(`${payload.errorMsg}` ? `${payload.errorMsg}` : '删除失败！');
      }
      return {
        ...state,
      };
    },
    // 处理搜索下垃
    selectList_r(state, action) {
      const { payload } = action;
      if (payload && payload.code === 0) {
        return {
          ...state,
          driverList: payload.result,
        };
      } else {
        message.error('获取客户下拉框数据失败！');
      }
    },
  },
};
